/***************************** BEGIN LICENSE BLOCK ***************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/MPL-1.1.html
 
 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the License.
 
 The Original Code is the "EO-SPS Framework".
 
 The Initial Developer of the Original Code is Spotimage S.A.
 Portions created by the Initial Developer are Copyright (C) 2007
 the Initial Developer. All Rights Reserved.
 
 Contributor(s): 
    Alexandre Robin <alexandre.robin@spotimage.fr>
 
******************************* END LICENSE BLOCK ***************************/

package com.spotimage.eosps;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.vast.ows.OWSException;
import org.vast.ows.OWSExceptionReport;
import org.vast.ows.sps.SPSException;
import org.vast.ows.swe.SWERequestReader;
import org.vast.xml.DOMHelper;


/**
* <p><b>Title:</b><br/>
* EO-SPS Validate Request Reader v2.0
* </p>
*
* <p><b>Description:</b><br/>
* Provides methods to parse a KVP or XML EO-SPS Validate
* request and create a ValidateRequest object for version 2.0
* </p>
*
* <p>Copyright (c) 2010</p>
* @author Alexandre Robin <alexandre.robin@spotimage.fr>
* @date July, 19 2010
* @version 1.0
*/
public class ValidateRequestReaderV20 extends SWERequestReader<ValidateRequest>
{

	@Override
	public ValidateRequest readURLQuery(String queryString) throws OWSException
	{
		throw new SPSException(noKVP + "SPS 2.0 Validate");
	}


	@Override
	public ValidateRequest readXMLQuery(DOMHelper dom, Element requestElt) throws OWSException
	{
		ValidateRequest request = new ValidateRequest();

		// do common stuffs like version, request name and service type
		readCommonXML(dom, requestElt, request);

		// taskID
		String taskID = dom.getElementValue(requestElt, "task");
		request.setTaskID(taskID);
		
		// segmentIDs
		NodeList segmentIDElts = dom.getElements(requestElt, "segmentID");
		for (int i=0; i<segmentIDElts.getLength(); i++)
		{
			String segID = dom.getElementValue((Element)segmentIDElts.item(i));
			request.getSegmentIDs().add(segID);
		}
		
		checkParameters(request, new OWSExceptionReport());
		return request;
	}


	/**
	 * Checks that Validate mandatory parameters are present
	 * @param request
	 * @throws OWSException
	 */
	protected void checkParameters(ValidateRequest request, OWSExceptionReport report) throws OWSException
	{
		// check common params + generate exception
		checkParameters(request, report, EOSPSUtils.EOSPS);

		// Check that task ID is present
		if (request.getTaskID() == null)
			report.add(new OWSException(OWSException.missing_param_code, "task"));
		
		// Check that at least one segment ID is present
		if (request.getSegmentIDs() == null || request.getSegmentIDs().isEmpty())
			report.add(new OWSException(OWSException.missing_param_code, "segmentID"));

		report.process();
	}
}